<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/1px.css" />
		<link rel="stylesheet" href="../fonts/iconfont.css" />
		<style>
			html {
				font-family: Roboto, Arial, "Noto Sans CJK SC", "Noto Sans CJK TC", "Pingfang SC", "Microsoft Yahei", "WenQuanYi Micro Hei", sans-serif;
			}
			
			body {
				overflow-x: hidden;
				background-color: #fff !important;
			}
			
			.mui-content {
				font-size: 13px;
				background-color: #fff;
			}
			
			.flex {
				display: flex;
				margin-top: 5px;
			}
			
			.flex-item {
				flex: 1;
			}
			
			.mui-select {
				width: 80%;
				margin-left: 10%;
				border: 1px solid #C0C0C0;
				border-radius: 6px;
				line-height: 24px;
				font-size: 14px;
				color: #ACACB4;
				text-indent: 6px;
			}
			
			.mui-select:before {
				top: 2px;
				right: 5px;
			}
			
			.add-goods-type {
				position: relative;
				font-size: 13px;
				height: 38px;
				line-height: 38px;
				padding-top: 0;
				margin-top: 0;
			}
			
			.apply-id {
				margin-left: 15px;
			}
			/*列表*/
			
			.goods-list {
				list-style: none;
			}
			
			.bgGray {
				background-color: #F8F8F8;
			}
			
			.goods-each {
				padding: 5px 5px;
				position: relative;
			}
			
			.title-box {
				background-color: #F8F8F8;
				height: 30px;
				line-height: 30px;
			}
			
			.img-box-title {
				flex: 2;
				text-align: center;
			}
			
			.number-title {
				flex: 4;
				text-align: center;
			}
			
			.op-title {
				flex: 3;
				text-align: center;
			}
			
			.img-box {
				flex: 2;
				position: relative;
			}
			
			.img-box img {
				height: 70px;
				width: 70px;
				vertical-align: middle;
				text-align: center;
			}
			
			.img-box .id {
				position: absolute;
				bottom: 1px;
				width: 100%;
				background-color: rgba(52, 52, 52, .5);
				color: #FFFFFF;
				text-align: center;
				font-size: 13px;
			}
			
			.goods-info-box {
				flex: 4;
				font-size: 14px;
				line-height: 24px;
			}
			
			.goods-info-box div {
				margin-left: 5px;
			}
			
			.goods-op-box {
				flex: 3;
				position: relative;
			}
			/*申请*/
			
			.apply-info {
				flex: 2;
				font-size: 14px;
				line-height: 24px;
			}
			
			.max-apply {
				margin-left: 5px;
				margin-bottom: 2px;
			}
			
			.apply-num {
				font-weight: 700;
				position: absolute;
				bottom: 2px;
				right: 4px;
			}
			/**/
			
			.bottom-change-box {
				padding: 2px;
				position: relative;
			}
			
			.bottom-change-box:before {
				content: '';
				height: 1px;
				position: absolute;
				right: 0;
				bottom: 28px;
				width: 100%;
				transform: scaleY(.5);
				background-color: #CCCCCC;
			}
			
			.bottom-change-box .choose-btn {
				width: 60px;
				height: 24px;
				line-height: 12px;
				float: right;
				background-color: #FF9800;
				border-color: #FF9800
			}
			
			.bottom-change-box .choose-icon {
				display: inline-block;
				float: right;
				width: 24px;
				color: #C0BFC4;
			}
			/*滚动区*/
			
			.mui-scroll-wrapper {
				top: 39px;
				bottom: 43px;
			}
			/*底部*/
			
			footer {
				height: 42px;
				line-height: 42px;
				background-color: #fff;
				position: absolute;
				bottom: 0;
				width: 100%;
				text-align: center;
				font-size: 15px
			}
			
			footer:before {
				content: '';
				height: 1px;
				position: absolute;
				right: 0;
				bottom: 42px;
				width: 100%;
				transform: scaleY(.5);
				background-color: #acacb4;
			}
			
			.back-btn {
				background-color: #E6E6E6;
				color: #000;
				width: 90%;
				margin: auto auto;
				vertical-align: middle;
			}
			
			.next-btn {
				background-color: #ff6800;
				color: #fff;
				width: 90%;
				margin: auto auto;
				vertical-align: middle;
			}
			/**/
			
			.mui-popover {
				height: 100px;
			}
			
			.mui-popover .mui-scroll-wrapper {
				top: 0;
				bottom: 0;
				padding: 10px;
			}
		</style>
	</head>

	<body>
		<div class="add-goods-type vux-1px-b">
			<span class="apply-id">进货单号：</span>
		</div>
		<div class="mui-content">
			<!---->
			<div id="refreshContainer" class="mui-scroll-wrapper">
				<div class="mui-scroll">
					<ul class="goods-list mui-table-view mui-table-view-chevron">
						<script id="goodsLi" type="text/html">
							<div class="flex title-box">
								<div class="img-box-title">礼品</div>
								<div class="number-title">数量</div>
								<div class="op-title">操作</div>
							</div>
							<% for(var i=0;i<list.length;i++ ){ %>
							<%if(i%2===0){%>
							<li class="goods-each mui-clearfix">
								<%}else {%>
								<li class="goods-each bgGray mui-clearfix">
									<%}%>
									<div class="flex goods-flex">
										<div class="img-box">
											<a href="#middlePopover" class="img-info" index="<%=i%>"><img src="<%=imgPath+list[i].cutImg%>" alt="" /></a>
											<div class="id">
												<%=list[i].goodsId%>
											</div>
										</div>
										<div class="goods-info-box">
											<div class="goods-name">
												<%=list[i].goodsName%>
											</div>
											<div class="goods-id">门店库存：
												<%=list[i].shopAmount%>
											</div>
											<div class="goods-num">
												最大申请数：
												<%=list[i].wareHouseGoodsAmount%>
											</div>
										</div>
										<div class="goods-op-box">
											<span class="apply-num">申请数：<%=list[i].amount%></span>
										</div>
									</div>
									<div class="bottom-change-box mui-clearfix">
										<div class="mui-btn mui-btn-primary choose-btn" index=<%=i %> data-id="<%=list[i].goodsId%>">调整</div>
										<i class="iconfont icon-delete choose-icon" index=<%=i%> data-id="<%=list[i].goodsId%>"></i>
									</div>
								</li>
								<%}%>
						</script>
					</ul>
				</div>
			</div>
		</div>
		<!--底部-->
		<footer class="flex btn-box">
			<div class="flex-item">
				<div class="mui-btn back-btn mui-action-back">取消订单</div>
			</div>
			<div class="flex-item">
				<div class="mui-btn next-btn">提交订单</div>
			</div>
		</footer>
		<!---->
		<!--显示附加信息-->
		<div id="middlePopover" class="mui-popover">
			<div class="mui-popover-arrow"></div>
			<div class="mui-scroll-wrapper">
				<div class="mui-scroll">
					<p class="fullName">规格：<span></span></p>
					<p class="tags">用途：<span></span></p>
					<p class="score">积分：<span></span></p>
				</div>
			</div>
		</div>
		<!---->
	</body>
	<script src="../config.js"></script>
	<script src="../js/template-native.js" type="text/javascript" charset="utf-8"></script>
	<script src="../js/mui.min.js"></script>
	<script>
		(function($, doc) {
			$.init({
				beforeback: function() {
					//获得列表界面的webview
					var purchase1 = plus.webview.getWebviewById('purchase')
					$.fire(purchase1, 'delete', {
						applyGoodsList: applyGoods.applyGoodsList,
						applyId: applyGoods.apply.applyId
					});
					//返回true，继续页面关闭逻辑
					return true;
				},
				pullRefresh: {
					container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等					
					//					up: {
					//						height:'50px',
					//						auto:true,
					//						contentrefresh: '正在加载...',
					//						contentnomore:'没有更多数据了',
					//						callback: pullupRefresh
					//					}
				}
			})
			$.plusReady(function() {
				receiveParams()
			})
			$.ready(function() {
				bindEvent()
			})
			var applyGoods = {
					apply: {
						applyId: '',
						applyStatus: 2,
						storeId: '',
						storeName: ''
					},
					applyGoodsList: []
				},
				tags = [],
				types = []

			function pullupRefresh() {
				setTimeout(function() {
					$('#pullrefresh').pullRefresh().endPulldownToRefresh();
				}, 2000)
			}
			//渲染页面
			function setPage(goodsList) {
				var html = template('goodsLi', {
					list: goodsList,
					imgPath: config.imgPath
				})
				$('.goods-list')[0].innerHTML = html
			}
			//接收传值
			function receiveParams() {
				var self = plus.webview.currentWebview();
				currentShopObj = self.currentShopObj;
				applyGoods.apply.storeId = self.currentShopObj.storeId
				applyGoods.apply.storeName = self.currentShopObj.storeName
				applyGoods.apply.applyId = self.applyId
				applyGoods.applyGoodsList = self.applyGoodsList
				tags = self.tags
				types = self.types
				setTimeout(function() {
					document.querySelector(".apply-id").innerHTML = '进货单号：' + self.applyId
					setPage(applyGoods.applyGoodsList)
				}, 0)
			}

			//绑定
			function bindEvent() {
				//滚动
				$('.mui-scroll-wrapper').scroll({
					scrollY: true,
					startX: 0, //初始化时滚动至x
					startY: 0, //初始化时滚动至y
					bounce: true,
					deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006，
				});
				//点击调整
				$(".goods-list").on('tap', '.choose-btn', function(e) {
					e.detail.gesture.preventDefault(); //修复iOS 8.x平台存在的bug，使用plus.nativeUI.prompt会造成输入法闪一下又没了
					var btnArray = ['取消', '确定'];
					var goodsId = this.getAttribute('data-id')
					var index = this.getAttribute('index')
					mui.prompt('请输入申请的数量：', applyGoods.applyGoodsList[index].amount, '调整', btnArray, function(e) {
						if(e.index == 1) {
							if(e.value) {
								applyGoods.applyGoodsList[index].amount = e.value * 1
								document.getElementsByClassName("apply-num")[index].innerText = '申请数：' + e.value
							}
						} else {}
					}, 'div')
					document.querySelector('.mui-popup-input input').type = 'number'
				});

				//显示用途
				$('.goods-list').on('tap', '.img-info', function(e) {
					var ind = this.getAttribute('index')
					if(applyGoods.applyGoodsList[ind].type) {
						var typeFullName = applyGoods.applyGoodsList[ind].type
					} else {
						var typeFullName = applyGoods.applyGoodsList[ind].typeFullName
					}
					var tags = ''
					applyGoods.applyGoodsList[ind].tags.forEach(function(item) {
						tags += item.tagDescribe + ' '
					})
					document.querySelector(".fullName span").innerText = typeFullName
					document.querySelector(".tags span").innerText = tags
					document.querySelector(".score span").innerText = applyGoods.applyGoodsList[ind].score
				})
				//删除
				$(".goods-list").on('tap', '.choose-icon', function(e) {
					e.detail.gesture.preventDefault();
					var goodsId = this.getAttribute('data-id')
					var index = this.getAttribute('index')
					var btnArray = ['否', '是'];
					mui.confirm('删除货品', '', btnArray, function(e) {
						if(e.index == 1) {
							applyGoods.applyGoodsList.splice(index, 1)
							setPage(applyGoods.applyGoodsList)
						} else {}
					})
				})
				//		提交
				document.querySelector(".next-btn").addEventListener('tap', function() {
					var purchseList = plus.webview.getWebviewById('purchseList')
					$.fire(purchseList, 'refresh');
					if(applyGoods.applyGoodsList.length > 0) {
						config.ajax({
							type: "post",
							url: config.basePath + "/apply/writeDraft",
							async: true,
							headers: {
								'Content-Type': 'application/json'
							},
							data: applyGoods,
							success: function(res) {
								if(res.result === 1) {
									//刷新列表页面
									var purchaseList = plus.webview.getWebviewById('purchaseList')
									if(purchaseList) {
										$.fire(purchaseList, 'changeList', '');
									}
									//
									var titleText = currentShopObj.storeName + '-进货'
									$.openWindow({
										url: 'purchaseList.html',
										id: 'purchaseList',
										styles: {
											top: 0, //新页面顶部位置
											bottom: 0, //新页面底部位置
											titleNView: {
												titleText: titleText,
												titleColor: "#FFFFFF",
												titleSize: "17px",
												backgroundColor: "#FF6800",
												autoBackButton: true,
												buttons: [{
													float: "right",
													fontSize: "18px",
													fontSrc: "_www/fonts/iconfont.ttf", //wap2app内置字体文件
													text: "\ue602",
													onclick: function() {
														var allPage = plus.webview.all()
														var nowPage = plus.webview.getWebviewById('index')
														for(var i = 0; i < allPage.length; i++) {
															if(allPage[i].getURL() !== nowPage.getURL()) {
																plus.webview.close(allPage[i]);
															}
														}
													}
												}],
												splitLine: {
													color: "#CCCCCC",
													height: "1px"
												}
											}
										},
										extras: {
											currentShopObj: currentShopObj,
											type: 2
										}
									})
								}

							}
						});

					} else {
						$.toast('请选择货品');
					}

				})
			}

		}(mui, document))
	</script>
	</body>

</html>